<template>
  <div>
    <el-dialog
      :title="isAudit ? '审核信息' : editInfoForm.fileType == 1 ? '文件夹信息' : fileTypeStr[mediaType]"
      :visible.sync="show"
      width="40%"
      :before-close="handleClose">
      <div>
        <el-form ref="form" :model="editInfoForm" :label-width="mediaType == 4 ? '140px' : '80px'">
          <el-form-item label="名称">
            <el-input v-model="editInfoForm.mediaName"></el-input>
          </el-form-item>
          <el-form-item label="网址" v-if="editInfoForm.fileType == 2 && (mediaType == 5 || mediaType == 9)">
            <el-input v-model="editInfoForm.location"></el-input>
          </el-form-item>
          <el-form-item label="路径" v-if="editInfoForm.fileType == 2 && mediaType == 4">
            <el-input v-model="editInfoForm.location"></el-input>
          </el-form-item>
          <el-form-item label="关闭时要清理的进程" v-if="editInfoForm.fileType == 2 && mediaType == 4">
            <el-input v-model="editInfoForm.cleanProcess"></el-input>
            <div>
              注意：添加的应用必须受控终端上提前安装。
            </div>
          </el-form-item>
          <el-form-item label="缩略图" v-if="editInfoForm.fileType == 2 && mediaType != 2">
            <template v-if="!editInfoForm.image">
              <el-upload
                size="mini"
                style="width: 100%;"
                class="upload-demo"
                drag
                :on-success="uploadImageSuccess"
                :action="uploadUrl"
                list-type="picture"
                :multiple="multiple"
                :accept="imageFormat"
                @drop.native="e => beforeUploadImage(e.dataTransfer.files[0])"
              >
                <i class="el-icon-upload"></i>
                <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
              </el-upload>
            </template>
            <div v-else>
              <div @click="delImageIcon" class="el-icon-circle-close close-file-icon deleteFile"></div>
              <el-image
                style="width: 100px; height: 100px"
                :src="editInfoForm.image"
                fit="cover">
              </el-image>
            </div>
          </el-form-item>
          <el-form-item label="文件" v-if="editInfoForm.fileType == 2 && mediaType != 5 && mediaType != 9">

            <template v-if="!editInfoForm.mediaLink">
              <el-upload
                size="mini"
                style="width: 100%;"
                class="upload-demo"
                drag
                :on-success="uploadFileSuccess"
                :action="uploadUrl"
                list-type="picture"
                :multiple="multiple"
                :accept="mediaType == 1 ? videoFormat : mediaType == 2 ? imageFormat : mediaType == 3 ? officeFormat : ''"
                @drop.native="e => beforeUploadFile(e.dataTransfer.files[0])"
              >
                <i class="el-icon-upload"></i>
                <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
              </el-upload>
            </template>
            <div v-if="editInfoForm.mediaLink && mediaType != 2">
              <span class="fileName">{{editInfoForm.mediaName}}.{{editInfoForm.mediaSuffix}}</span>
              <div @click="delFileIcon" class="el-icon-circle-close close-file-icon deleteFileName"></div>
            </div>
            <div v-if="editInfoForm.mediaLink && mediaType == 2">
              <div @click="delFileIcon" class="el-icon-circle-close close-file-icon deleteFile"></div>
              <el-image
                style="width: 100px; height: 100px"
                :src="editInfoForm.mediaLink"
                fit="cover">
              </el-image>
            </div>
          </el-form-item>
          <el-form-item label="分组">
            <el-select
              v-model="editInfoForm.groupId"
              placeholder="组名"
              clearable
            >
              <el-option
                v-for="(item, index) in groupList"
                :label="item.group_name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="审核记录">
            <el-timeline>
                <el-timeline-item
                  v-for="(activity, index) in activities"
                  :key="index"
                  :timestamp="activity.timestamp">
                  <a style="color: #409eff;">{{activity.audit}}</a>
                  <p>{{activity.remark}}</p>
                </el-timeline-item>
              </el-timeline>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="confirmUpdateFileInfo">{{editInfoForm.audit != null && editInfoForm.audit == 2 ? "重新审核" : "确 定"}}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import * as distributeService from "@/views/distribute/service";
import * as service from './service'

export default {
  name: "index",
  props: {
    show: {
      type: Boolean,
      default: false,
    },
    mediaType: {
      type: [Number, String],
      default: ''
    },
    isAudit: {
      type: Boolean,
      default: false
    },
    editInfoForm: {
      type: Object,
      default: () => {
        return {
          mediaName: '',
          location: '',
          image: '',
          mediaLink: '',
          groupId: '',
          mediaSuffix: '',
          fileType: '',
          cleanProcess: '',
          auditHistory: []
        }
      }
    }
  },
  data() {
    return {
      multiple: false,
      activities: [],
      //1视频 2图片 3文档 4应用程序 5网页应用 6电脑桌面 7其他 8节目 9流媒体
      fileTypeStr:{
        "1": "视频信息",
        "2": "图片信息",
        "3": "文档信息",
        "4": "应用信息",
        "5": "网页信息",
        "6": "电脑桌面信息",
        "7": "其他信息",
        "8": "节目信息",
        "9": "流媒体信息",
      },
      imageFormat: ".jpg,.png,.jpeg,.JPG,.PNG,.JPEG",
      videoFormat: ".mp4,.avi,.dat,.dvr,.vcd,.mov,.svcd,.vob,.dvd,.dvtr,.bbc,.evd,.flv,.rmvb,.wmv,.mkv,.3gp,.rmvb",
      officeFormat: ".xlsx,.pdf,.ppt,.pptx,.xls,.doc,.docx,.dotx,.dotm,.ppsx,.pptm,.xps",
      groupList: [],
      uploadUrl: window.ajaxUploadUrl + '/common/fileApi/file',
    }
  },
  watch: {
    show(val) {
      if(val) {
        this.getGroupData();
        console.log('editInfoForm ---', this.mediaType);
        this.initTimeline();
      }
    }
  },
  mounted() {
  },
  methods: {
    initTimeline(){
      this.activities = [];
      const history = this.editInfoForm.auditHistory;
      if(history && history.length > 0) {
        history.forEach(item => {
          let audit = item.audit == 0 ? "待审核" : item.audit == 1 ? "审核通过" : item.audit == 2 ? "审核不通过" : "";
          let obj = {
            audit: audit,
            remark: item.remark,
            timestamp: item.update_date
          }
          this.activities.push(obj);
        })
      }
    },
    beforeUploadImage(file) {
      var suffix = "."+file.name.substring(file.name.lastIndexOf(".") + 1);
      if(!this.imageFormat.includes(suffix)){
        this.$message.error('文件格式错误')
        return false;
      }
      return true;
    },
    beforeUploadFile(file) {
      var suffix = "."+file.name.substring(file.name.lastIndexOf(".") + 1);
      if(this.mediaType == 1) {
        if(!this.videoFormat.includes(suffix)){
          this.$message.error('文件格式错误')
          return false;
        }
      } else if(this.mediaType == 2) {
        if(!this.imageFormat.includes(suffix)){
          this.$message.error('文件格式错误')
          return false;
        }
      } else if(this.mediaType == 3) {
        if(!this.officeFormat.includes(suffix)){
          this.$message.error('文件格式错误')
          return false;
        }
      }
      return true;
    },
    getGroupData() {
      const params = {
        page: 1,
        pageSize: 200000000,
        mediaType: this.mediaType
      }

      distributeService.getFileGroupList(params).then((res) => {
        this.groupList = res.data.records
      })
    },
    uploadImageSuccess(res, file) {
      this.editInfoForm.image = res.data[0].file
    },
    uploadFileSuccess(res, file) {
      this.editInfoForm.mediaLink = res.data[0].file
      let data = res.data[0].file;
      let fileArr = data.split(".");
      let mediaSuffix = "";
      if(fileArr.length > 0) {
        mediaSuffix = fileArr[fileArr.length-1];
      }
      this.editInfoForm.mediaSuffix = mediaSuffix;
      this.editInfoForm.mediaName = res.data[0].fileName;
    },
    delImageIcon() {
      this.editInfoForm.image = ''
    },
    delFileIcon() {
      this.editInfoForm.mediaLink = ''
    },
    handleClose() {
      this.$emit('on-close')
    },
    confirmUpdateFileInfo() {
      const params = Object.assign({}, this.editInfoForm)
      // if(params.mediaSuffix) {
      //   params.mediaName = `${params.mediaName}.${params.mediaSuffix}`
      // }
      delete params.auditHistory
      service.saveFileContent(params).then(() => {
        this.$message.success('修改成功')
        this.handleClose()
      })
    }
  }
}
</script>

<style scoped>
  .deleteFile{
    position: absolute;
    z-index: 1;
    left: 90px;
    top: -5px;
    cursor: pointer;
    color: red;
  }
  .fileName{
    color: rgba(79, 138, 255, 1);
    font-style: italic;
  }
  .deleteFileName{
    display: inline;
    margin-left: 10px;
    cursor: pointer;
    color: red;
  }
</style>
